package com.tx.admin.noveldict.service.impl;

import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.tx.admin.noveldictdetail.pojo.dto.NovelDictDetailUpdateDTO;
import com.tx.admin.noveldictdetail.pojo.po.NovelDictDetail;
import com.tx.admin.noveldictdetail.pojo.vo.NovelDictDetailVO;
import com.tx.admin.noveldictdetail.service.NovelDictDetailService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.tx.admin.noveldict.service.NovelDictService;
import com.tx.admin.noveldict.pojo.po.NovelDict;
import com.tx.admin.noveldict.mapper.NovelDictMapper;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.core.query.QueryChain;
import com.tx.core.common.enums.CommonResponseEnum;
import com.tx.core.util.PageUtils;
import com.tx.core.util.BeanCopyUtils;
import com.tx.core.util.Utils;
import com.tx.core.common.entity.PageResult;
import com.tx.core.common.entity.SelectIdsDTO;
import java.io.Serializable;
import java.util.List;
import com.tx.admin.noveldict.pojo.dto.NovelDictCreateDTO;
import com.tx.admin.noveldict.pojo.dto.NovelDictUpdateDTO;
import com.tx.admin.noveldict.pojo.dto.NovelDictListDTO;
import com.tx.admin.noveldict.pojo.dto.NovelDictImportDTO;
import com.tx.core.common.entity.ImportExcelDTO;
import com.tx.excel.core.ExcelResult;
import java.io.OutputStream;
import jakarta.servlet.http.HttpServletResponse;
import com.tx.core.util.FileUtils;
import com.tx.excel.utils.ExcelUtils;
import lombok.SneakyThrows;
import com.tx.admin.noveldict.pojo.vo.NovelDictVO;

import static com.tx.admin.noveldict.pojo.po.table.NovelDictTableDef.NOVEL_DICT;
import static com.tx.admin.noveldictdetail.pojo.po.table.NovelDictDetailTableDef.NOVEL_DICT_DETAIL;

/**
 * <p>
 * 字典 服务实现类
 * </p>
 *
 * @author sz-admin
 * @since 2025-05-06
 */
@Service
@RequiredArgsConstructor
public class NovelDictServiceImpl extends ServiceImpl<NovelDictMapper, NovelDict> implements NovelDictService {

    @Autowired
    private NovelDictDetailService dictDetailService;

    @Override
    public void create(NovelDictCreateDTO dto){
        NovelDict novelDict = BeanCopyUtils.copy(dto, NovelDict.class);
        save(novelDict);
    }

    @Override
    public void update(NovelDictUpdateDTO dto){
        NovelDict novelDict = BeanCopyUtils.copy(dto, NovelDict.class);
        QueryWrapper wrapper;
        // id有效性校验
        wrapper = QueryWrapper.create()
            .eq(NovelDict::getId, dto.getId());
        CommonResponseEnum.INVALID_ID.assertTrue(count(wrapper) <= 0);

        saveOrUpdate(novelDict);
    }

    @Override
    public PageResult<NovelDictVO> page(NovelDictListDTO dto){
        Page<NovelDictVO> page = pageAs(PageUtils.getPage(dto), buildQueryWrapper(dto), NovelDictVO.class);
        return PageUtils.getPageResult(page);
    }

    @Override
    public List<NovelDictVO> list(NovelDictListDTO dto){
        return listAs(buildQueryWrapper(dto), NovelDictVO.class);
    }

    @Override
    public void remove(SelectIdsDTO dto){
        CommonResponseEnum.INVALID_ID.assertTrue(dto.getIds().isEmpty());
        removeByIds(dto.getIds());
    }

    @Override
    public NovelDictVO detail(Object id){
        NovelDict novelDict = getById((Serializable) id);
        CommonResponseEnum.INVALID_ID.assertNull(novelDict);
        return BeanCopyUtils.copy(novelDict, NovelDictVO.class);
    }

    @SneakyThrows
    @Override
    public void importExcel(ImportExcelDTO dto) {
        ExcelResult<NovelDictImportDTO> excelResult = ExcelUtils.importExcel(dto.getFile().getInputStream(), NovelDictImportDTO.class, true);
        List<NovelDictImportDTO> list = excelResult.getList();
        List<String> errorList = excelResult.getErrorList();
        String analysis = excelResult.getAnalysis();
        System.out.println(" analysis : " + analysis);
        System.out.println(" isCover : " + dto.getIsCover());
    }

    @SneakyThrows
    @Override
    public void exportExcel(NovelDictListDTO dto, HttpServletResponse response) {
        List<NovelDictVO> list = list(dto);
        String fileName = "字典模板";
        OutputStream os = FileUtils.getOutputStream(response, fileName + ".xlsx");
        ExcelUtils.exportExcel(list, "字典", NovelDictVO.class, os);
    }

    @Override
    public void saveAll(NovelDictListDTO dto) {
        Long dictId = dto.getDetails().get(0).getDictId();
        NovelDictDetail novelDictDetail = new NovelDictDetail();
        novelDictDetail.setId(dictId);
        NovelDictUpdateDTO copy = BeanCopyUtils.copy(dto, NovelDictUpdateDTO.class);
        copy.setId(dictId);
        this.update(copy);
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("dict_id",dictId);
        dictDetailService.remove(wrapper);
        dictDetailService.saveBatch(dto.getDetails());
    }

    @Override
    public List<NovelDictVO> getDetailOption() {
        QueryWrapper wrapper = QueryWrapper.create().select(NOVEL_DICT.ID,NOVEL_DICT.CODE,NOVEL_DICT.NAME);
        return listAs(wrapper, NovelDictVO.class);
    }

    private static QueryWrapper buildQueryWrapper(NovelDictListDTO dto) {
        QueryWrapper wrapper = QueryWrapper.create().from(NovelDict.class);
        if (Utils.isNotNull(dto.getName())) {
            wrapper.like(NovelDict::getName, dto.getName());
        }
        if (Utils.isNotNull(dto.getCode())) {
            wrapper.eq(NovelDict::getCode, dto.getCode());
        }
        return wrapper;
    }
}